package cn.firecode.data_collect.controller;


import cn.firecode.data_collect.domain.entity.CollectRobotT;
import cn.firecode.data_collect.service.CollectRobotTService;
import cn.firecode.data_collect.utils.DataResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangfan
 * @since 2019-12-10
 */
@RestController
@RequestMapping("/robot")
public class CollectRobotTController {

    @Autowired
    private CollectRobotTService robotTService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public DataResponse add(String type, String name, String manufacturer, String softwareVersion){
        CollectRobotT robotT = new CollectRobotT();
        robotT.setCreateTime(LocalDate.now());
        robotT.setRobotManufacturer(manufacturer);
        robotT.setRobotName(name);
        robotT.setIsDelete(0);
        robotT.setRobotType(type);
        robotT.setSoftwareVersion(softwareVersion);
        boolean result = robotTService.save(robotT);
        if(result){
            return DataResponse.succeed("新增成功");
        }else{
            return DataResponse.failed(1, "新增失败");
        }
    }

    @RequestMapping(value = "/update/{robotId}", method = RequestMethod.POST)
    public DataResponse update(@PathVariable("robotId") Integer robotId,  String type, String name, String manufacturer, String softwareVersion){
        CollectRobotT robotT = new CollectRobotT();
        robotT.setRobotId(robotId);
        robotT.setRobotManufacturer(manufacturer);
        robotT.setRobotName(name);
        robotT.setRobotType(type);
        robotT.setSoftwareVersion(softwareVersion);

        boolean result = robotTService.saveOrUpdate(robotT);
        if(result){
            return DataResponse.succeed("修改成功");
        }else{
            return DataResponse.failed(1, "修改失败");
        }
    }

    @RequestMapping(value = "/delete/{robotId}", method = RequestMethod.GET)
    public DataResponse delete(@PathVariable("robotId") Integer robotId){
        CollectRobotT robotT = robotTService.getById(robotId);
        if(robotT==null){
            return DataResponse.failed(1,"删除错误，无有效记录");
        }
        robotT.setIsDelete(1);
        boolean result = robotTService.saveOrUpdate(robotT);
        if(result){
            return DataResponse.succeed("删除成功");
        }else{
            return DataResponse.failed(1,"删除失败");
        }
    }

    @RequestMapping(value = "/get/{robotId}", method = RequestMethod.GET)
    public DataResponse getById(@PathVariable("robotId")Integer robotId){
        CollectRobotT byId = robotTService.getById(robotId);
        if(byId!=null){
            return DataResponse.succeed("查询成功",byId);
        }
        return DataResponse.failed(1,"查询失败，没有查到");

    }

    @RequestMapping(value = "/getList" , method = RequestMethod.GET)
    public DataResponse getList(){
        QueryWrapper<CollectRobotT> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        List<CollectRobotT> list = robotTService.list(wrapper);
        return DataResponse.succeed(String.valueOf(list.size()),list);
    }
}
